# 第一阶段：构建环境（使用与服务器兼容的ARM64基础镜像）
FROM arm64v8/centos:7 AS builder

# 设置与服务器一致的环境变量
# Sets the default system language environment to UTF-8 encoded English (United States) locale
ENV LANG=en_US.UTF-8 

# 步骤1：更换国内源
RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup && \
    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-altarch-7.repo && \
    sed -i 's/\$basearch/aarch64/g' /etc/yum.repos.d/CentOS-Base.repo && \
    yum clean all && \
    yum makecache

# 步骤2：安装基础工具
RUN yum install -y \
    wget sudo which \
    make gcc-c++ bison\
    gmp-devel mpfr-devel libmpc-devel \
    perl perl-core \
    zlib-devel bzip2-devel xz-devel libffi-devel\
    mesa-libGL-devel

COPY  sys_deps/ /tmp/

# 安装make 4.0
RUN cd /tmp &&\
    wget -nc https://mirrors.aliyun.com/gnu/make/make-4.0.tar.gz &&\
    tar -xzf make-4.0.tar.gz &&\
    cd make-4.0 &&\
    ./configure --prefix=/usr/local/make-4.0 &&\
    make -j$(nproc) &&\
    make install &&\
    # 创建软连接
    ln -sf  /usr/local/make-4.0/bin/make  /usr/bin/make

# 步骤4：安装GCC 9.2.0
RUN  cd /tmp &&\
    wget -nc https://mirrors.aliyun.com/gnu/gcc/gcc-9.2.0/gcc-9.2.0.tar.gz &&\
    tar xvf gcc-9.2.0.tar.gz &&\
    cd gcc-9.2.0 &&\
    cd .. &&\
    mkdir gcc-build &&\   
    cd gcc-build && ../gcc-9.2.0/configure \
    --prefix=/usr/local/gcc-9.2 \
    --enable-languages=c,c++ \
    --with-system-zlib \
    --disable-multilib \
    --disable-bootstrap \
    --with-arch=armv8-a &&\
    make -j$(nproc) &&\
    make install &&\
    echo 'export PATH=/usr/local/gcc-9.2/bin:$PATH' | tee /etc/profile.d/gcc.sh &&\
    echo 'export LD_LIBRARY_PATH=/usr/local/gcc-9.2/lib64:$LD_LIBRARY_PATH' | tee -a /etc/profile.d/gcc.sh &&\
    source /etc/profile.d/gcc.sh &&\
    # 复制到系统库目录
    cp /usr/local/gcc-9.2/lib64/libstdc++.so.6.0.27 /usr/lib64/ &&\
    ln -sf /usr/lib64/libstdc++.so.6.0.27 /usr/lib64/libstdc++.so.6 
# 5. 验证安装
# RUN gcc --version
# RUN strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX


# 步骤3：安装openssl

RUN cd /tmp &&\
    tar -xzf openssl-1.1.1k.tar.gz &&\
    cd openssl-1.1.1k &&\
    ./config --prefix=/usr/local/openssl  &&\
    make -j$(nproc) &&\
    make install &&\
    ln -sf /usr/local/openssl/bin/openssl /usr/bin/openssl &&\
    echo '/usr/local/openssl/lib' | tee /etc/ld.so.conf.d/openssl-1.1.1k.conf &&\
    ldconfig
# 临时解决OpenSSL依赖库问题
# export LD_LIBRARY_PATH="/usr/local/openssl/lib:$LD_LIBRARY_PATH"


# 步骤3：安装Python 3.10
RUN cd /tmp && wget -nc https://mirrors.huaweicloud.com/python/3.10.13/Python-3.10.13.tar.xz &&\
    tar -xf Python-3.10.13.tar.xz  && \
    cd Python-3.10.13 && \
    # export LDFLAGS="-L/usr/local/openssl/lib" && \
    # export CPPFLAGS="-I/usr/local/openssl/include" && \
    # export LD_LIBRARY_PATH="/usr/local/openssl/lib:$LD_LIBRARY_PATH" &&\
    ./configure --prefix=/usr/local/python3.10 \
    --with-openssl=/usr/local/openssl \
    --enable-optimizations \
    --with-system-ffi && \
    make -j$(nproc) && \
    make install && \
    ln -s /usr/local/python3.10/bin/python3.10 /usr/bin/python3.10 && \
    ln -s /usr/local/python3.10/bin/pip3.10 /usr/bin/pip3.10 && \
    python3.10 -c "import ssl; print(ssl.OPENSSL_VERSION)" &&\
    python3.10 -c "import _ctypes; print('ctypes module loaded successfully')"


# 安装glibc到非系统目录
RUN cd /tmp  && source /etc/profile.d/gcc.sh && unset LD_LIBRARY_PATH && \
    gcc --version && \
    wget -nc https://mirrors.aliyun.com/gnu/glibc/glibc-2.28.tar.gz && \
    tar -xzf glibc-2.28.tar.gz && \
    cd glibc-2.28 && \
    mkdir build && \
    cd build && \
    ../configure --prefix=/usr/local/glibc-2.28 \
    --disable-werror && \
    make -j$(nproc) && \
    make install
    
# 设置工作目录
WORKDIR /app

# 设置非root用户（匹配服务器账号）
RUN groupadd -g 1000 txtdeal && \
    useradd -u 1000 -g 1000 -m txtdeal && \
    echo "txtdeal ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers && \
    echo "txtdeal ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/90-cloud-init-users && \
    chmod 0440 /etc/sudoers.d/90-cloud-init-users && \
    chown -R txtdeal:txtdeal /app && \
    chmod -R 755 /app